<template>
  <div class="asset-overview">
    <div
      class="overview-item total-assets"
      style="padding-top: 16px; padding-left: 16px"
    >
      <div class="title">资产总数</div>
      <div class="value">{{ assetData.totalAssets }}</div>
      <div class="indicators">
        <div class="indicator">
          <span>空置率</span>
          <span>{{ assetData.vacantRate }}</span>
        </div>
        <div class="indicator">
          <span>空置数</span>
          <span>{{ assetData.vacantTotal }}</span>
        </div>
      </div>
    </div>
    <div class="overview-item gauge" v-for="g in gaugeList" :key="g.key">
      <div class="title">{{ g.title }}</div>
      <div class="gauge-chart" :ref="(el) => (gaugeRefs[g.key] = el)"></div>
      <div class="gauge-legend">
        <span v-for="item in g.data" :key="item.name" class="legend-item">
          <span class="legend-dot" :style="{ background: item.color }"></span
          >{{ item.name }}
        </span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, nextTick } from "vue";
import * as echarts from "echarts";

// mock 数据
const assetData = {
  totalAssets: 3126,
  vacantRate: "12%",
  vacantTotal: 12,
};

const gaugeList = [
  {
    key: "rental",
    title: "价格比对预警",
    data: [
      { name: "评估", value: 10, color: "#3B82F6" },
      { name: "挂上", value: 15, color: "#22C55E" },
      { name: "挂下", value: 10, color: "#FBBF24" },
    ],
  },
  {
    key: "contract",
    title: "合同合规预警",
    data: [
      { name: "房屋", value: 10, color: "#3B82F6" },
      { name: "土地", value: 15, color: "#22C55E" },
      { name: "车位", value: 10, color: "#FBBF24" },
      { name: "其他", value: 10, color: "#F87171" },
    ],
  },
  {
    key: "vacancy",
    title: "空置监测预警",
    data: [
      { name: "房屋", value: 10, color: "#3B82F6" },
      { name: "土地", value: 15, color: "#22C55E" },
      { name: "车位", value: 10, color: "#FBBF24" },
      { name: "其他", value: 10, color: "#F87171" },
    ],
  },
  {
    key: "file",
    title: "文件缺失统计",
    data: [
      { name: "合同文件", value: 10, color: "#3B82F6" },
      { name: "评估文件", value: 15, color: "#22C55E" },
    ],
  },
];

const gaugeRefs = {};

function getGaugeOption(dataArr) {
  const total = dataArr.reduce((sum, cur) => sum + cur.value, 0);
  const pieData = [
    ...dataArr.map((item) => ({
      value: item.value,
      name: item.name,
      itemStyle: { color: item.color },
    })),
    {
      value: total,
      name: "",
      itemStyle: { color: "transparent" },
      label: { show: false },
      labelLine: { show: false },
    },
  ];
  return {
    series: [
      {
        type: "pie",
        startAngle: 180,
        radius: ["70%", "90%"],
        center: ["50%", "70%"],
        label: {
          show: true,
          position: "outside",
          formatter: function (params) {
            if (!params.data.name) return "";
            return params.data.value;
          },
          // color: '#222',
          fontSize: 12,
        },
        labelLine: {
          show: true,
          length: 10,
          length2: 10,
        },
        data: pieData,
      },
    ],
    grid: { left: 0, right: 0, top: 0, bottom: 0 },
  };
}

onMounted(() => {
  nextTick(() => {
    gaugeList.forEach((g) => {
      const chart = echarts.init(gaugeRefs[g.key]);
      chart.setOption(getGaugeOption(g.data));
      window.addEventListener("resize", () => chart.resize());
    });
  });
});
</script>

<style lang="scss" scoped>
.asset-overview {
  display: flex;
  gap: 12px;
  margin-bottom: 0;
  .overview-item {
    flex: 1;
    background: #fff;
    padding: 8px;
    border-radius: 8px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
    display: flex;
    flex-direction: column;
    min-height: 120px;
    .title {
      color: #222;
      font-size: 15px;
      margin-bottom: 8px;
      font-weight: 500;
    }
    .value {
      font-size: 32px;
      font-weight: 700;
      color: #1f2937;
      line-height: 1;
      margin-bottom: 8px;
    }
    .indicators {
      display: flex;
      gap: 16px;
      font-size: 13px;
      margin-top: auto;
      padding-top: 4px;
      .indicator {
        display: flex;
        align-items: center;
        gap: 4px;
      }
    }
    &.gauge {
      align-items: center;
      padding-left: 8px;
      padding-top: 16px;
      .gauge-chart {
        width: 100%;
        height: 100px;
        margin-bottom: 0;
      }
      .gauge-legend {
        display: flex;
        flex-wrap: wrap;
        gap: 10px;
        justify-content: center;
        .legend-item {
          font-size: 12px;
          color: #666;
          display: flex;
          align-items: center;
          .legend-dot {
            display: inline-block;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            margin-right: 4px;
          }
        }
      }
    }
  }
}
</style>
